package com.woniu.controller;

import com.woniu.been.entity.Code;
import com.woniu.been.entity.Exam;
import com.woniu.been.entity.Users;

import com.woniu.been.entity.vo.UserVo;
import com.woniu.mapper.UsersMapper;
import com.woniu.service.UserService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.Map;

@RestController
@RequestMapping("/UserAppLogin")
@CrossOrigin
public class UserController {


    @Autowired
    private UserService userService;
    @Autowired
    private UsersMapper  usersMapper;

    @Value("${wechat.appid}")
    private  String appId;
    @Value("${wechat.appsecret}")
    private  String appSecret;



    //用户小程序登录

    /**
     * 用户登录
     * @param users
     * @return
     */
    @PostMapping( "/login")
    public com.woniu.utils.ResponseResult<Object> login(@RequestBody Users users) {
        Users user = userService.login(users.getUsername(), users.getPassword());
        if (user != null){

            return com.woniu.utils.ResponseResult.success(user);
        }
        return com.woniu.utils.ResponseResult.fail(500,"用户名或密码错误");
    }
    //用户小程序注册
    @PostMapping("/register")
    public com.woniu.utils.ResponseResult<Object> register(@RequestBody Users users) {
        userService.register(users);

        return com.woniu.utils.ResponseResult.success();
    }
    //微信授权登录，通过微信账户提供的三个code参数解析用户信息（手机号）
    @PostMapping("/code")
    public com.woniu.utils.ResponseResult<Object> code1(@RequestBody Code code) {

        com.woniu.utils.WechatAuthService wechatAuthService = new com.woniu.utils.WechatAuthService(appId,  appSecret);

        Map<String, Object> loginInfo = wechatAuthService.getLoginInfo(code.getCode(), appId, appSecret);

        com.woniu.utils.WechatUtil2 wechatUtil = new com.woniu.utils.WechatUtil2();
        String phoneNumber = wechatUtil.getPhoneNumber(code.getEncryptedData(), loginInfo.get("session_key").toString(), code.getIv());
        Users users = usersMapper.findByPhone(phoneNumber);
        if (users == null){
            return com.woniu.utils.ResponseResult.fail(577,"用户不存在,请注册");
        }

        return com.woniu.utils.ResponseResult.success(users);
    }
    @PostMapping("/updateVoice")
    public com.woniu.utils.ResponseResult<Object> updateVoice(@RequestParam(value = "file",required = false)MultipartFile file) {

        if (file.isEmpty()) {
            return com.woniu.utils.ResponseResult.fail(399, "上传的文件为空");
        }

        //获取文件基本信息
        String originalFilename = file.getOriginalFilename();
        String contentType = file.getContentType();
        long size = file.getSize();

        System.out.println("文件名: " + originalFilename);
        System.out.println("文件类型: " + contentType);
        System.out.println("文件大小: " + size + " bytes");



        // 3. 返回成功响应
        return com.woniu.utils.ResponseResult.success("文件上传成功");
    }

    /**
     * 修改用户信息
     * @param users
     */
    @PostMapping("/update")
    public com.woniu.utils.ResponseResult update(@RequestBody Users users) {
        userService.update(users);
        return com.woniu.utils.ResponseResult.success();
    }

    @PostMapping("/exam")
    public com.woniu.utils.ResponseResult exam(@RequestBody Exam exam) {
//        System.out.println(exam);

        usersMapper.addexam(exam);
 
        return com.woniu.utils.ResponseResult.success();
    }

    @PostMapping("/updateById")
    public com.woniu.utils.ResponseResult updateById(@RequestBody Users user) {
        userService.updateById(user);
        return com.woniu.utils.ResponseResult.success();
    }

    /*
    * 查询所有 用户
    * */
    @PostMapping("/findAll")
    public com.woniu.utils.ResponseResult<com.woniu.utils.PageResult> findAll(@RequestBody UserVo userVo) {
        userService.findAll(userVo, userVo.getPageNum(), userVo.getPageSize());
        return com.woniu.utils.ResponseResult.success(userService.findAll(userVo, userVo.getPageNum(), userVo.getPageSize()));
    }

    /*
    * 根据id删除 用户
    * */
    @DeleteMapping("deletById")
    public com.woniu.utils.ResponseResult<Void> deletById(Integer userId) {
        userService.deleteById(userId);
        return com.woniu.utils.ResponseResult.success();
    }

    /*
    * 根据用户id查询用户数据
    * */
    @GetMapping("findById")
    public com.woniu.utils.ResponseResult<Users> findById(Integer userId) {
        Users user = userService.findById(userId);
        return com.woniu.utils.ResponseResult.success(user);
    }
}
